<?php
	include 'ZionHeader.inc';
?>
<HTML>
<head>
<title>ZionsAuto</title>

	<?php
		$name = $_SESSION['UName'];
		$accountType = $_SESSION['AType'];
		$password = $_POST['password'];
		
		
		//Need to compare $name with username of announcement
	?>

  <link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<BODY>
<h1><center>Announcements</center></h1>

<!-- Tab Menu Goes here -->
<div id="tabs">
  <ul>
    <li><a href="homePage.php"><span>Home</span></a></li>
    <li><a href="Browse.php"><span>Browse</span></a></li>
	<li><a href="announcement.php"><span>Announcements</span></a></li>
	<li><a href="postAnnouncement.php"><span>Post Announcement</span></a></li>
	<?php $AccountType = $_SESSION['AType'];
	if ($AccountType == 'R')
	{
		echo "<li id='dealerOnly'><a href='viewBookmark.php'><span>View Bookmarks</span></a></li>";
	}
	?>
	<li><a href="logout.php"><span>Log Out</span></a></li>
  </ul>
</div>



<br />

<?php

	$carScore = array();		//Our array which will hold name of vehicle and it's score in relevance
	$newArray = array();
	
	$first = 0;
	$second = 0;
	$third = 0;
	
	$firstID;
	$secondID;
	$thirdID;
	

	$AccountType = $_SESSION['AType'];
	include 'DatabaseConnect.inc';
	if ($AccountType == 'D'){
		$query1 = "SELECT * FROM Announcement where userID = '$name'";
		$result1 = mysql_query($query1);
		//while ($row = mysql_fetch_array( $result1, MYSQL_ASSOC)) {
		//$query2 = SELECT * FROM Vehicle where ID = '".$row['vehicle_ID']."'";
		//$result2 = mysql_query($query2); 
		//$row1 = mysql_fetch_array($result1);
		$query = "SELECT * FROM Vehicle where ID = '".$row1['vehicle_ID']."'";
	}
	else{
		$query = "SELECT * FROM Vehicle";
	}
	
	$result = mysql_query($query);
	
	echo "Hello: ".$name."<br />";
	//echo gettype($name);
	if(isset($_GET['detailsID']))
	{
		$flag=false;
	}
	else
	{
		$flag = true;
	}
	
	//If the view details button has not been clicked!
	if($flag)
	{
		echo "<table border='1'>";
		echo "<tr>  <th>Make</th> <th>Model</th> <th>Year</th> <th>Price</th>";
		echo "</tr>";
		if ($AccountType == 'D')
		{
			$table = $result1;
		}
		else
		{
			$table = $result;
		}
	
		// keeps getting the next row until there are no more to get
		while($row = mysql_fetch_array( $table ))
		{
			// Print out the contents of each row into a table
			// Queries to see if you own the car


			if ($AccountType=='D')
			{
				$query2 = "SELECT * FROM Vehicle where ID = '".$row['vehicle_ID']."'";
				$newResult = mysql_query($query2);
				$row2 = mysql_fetch_array($newResult); 		
			}
			else
			{
				$row2 = $row;
			}
			
			$queryCur = "SELECT * FROM Announcement WHERE userID = '$name' and vehicle_ID = '$row2[ID]'";
			$resultCur = mysql_query($queryCur);
			$rowCur = mysql_fetch_array($resultCur); 
			echo "<tr><td>"; 
			echo $row2['Make'];
			echo "</td><td>"; 
			echo $row2['Model'];
			echo "</td><td>"; 
			echo $row2['Year'];
			echo "</td><td>"; 
			echo $row2['Price'];
			echo "</td><td>"; 
			echo "<form mathod='get' action='announcement.php'><input type='hidden' value='".$row2['ID']."' name='detailsID' /><input type='submit' value='Show Details' /></form>";

			echo "</td></tr>";
		
		} 

		echo "</table>";
	}
	//View details button has been clicked!
	else
	{
		$queryDetails = "SELECT * FROM Vehicle WHERE ID='".$_GET['detailsID']."'";
		$resultDetails = mysql_query($queryDetails);
		$row2 = mysql_fetch_array($resultDetails);
		
		//echo $row2['ID'];
	
		echo "<table border='1'>";
		echo "<tr>  <th>Make</th> <th>Model</th> <th>Year</th> <th>Price</th> <th>VIN</th> <th>Cylinder</th> <th>Drive</th> <th>Color</th> <th>Description</th> <th>Mileage</th> <th>Number of Doors</th> <th>Photo</th>";
		$queryCur = "SELECT * FROM Announcement WHERE userID = '$name' AND vehicle_ID = '".$row2['ID']."'";
		$resultCur = mysql_query($queryCur);
		$rowCur = mysql_fetch_array($resultCur); 
		echo "<tr><td>"; 
		echo $row2['Make'];
		echo "</td><td>"; 
		echo $row2['Model'];
		echo "</td><td>"; 
		echo $row2['Year'];
		echo "</td><td>"; 
		echo $row2['Price'];
		echo "</td><td>"; 
		echo $row2['VIN'];
		echo "</td><td>"; 
		echo $row2['Cylinder'];
		echo "</td><td>"; 
		echo $row2['Drive'];
		echo "</td><td>"; 
		echo $row2['Color'];
		echo "</td><td>"; 
		echo $row2['Description'];
		echo "</td><td>"; 
		echo $row2['Mileage'];
		echo "</td><td>"; 
		echo $row2['NoDoors'];
		echo "</td><td>";
		echo "<img src='viewimage.php?id=".$row2['ID']."' width=250 height=200>";
		// Edit Button
		echo "</td><td>";
		if ($rowCur['vehicle_ID'] == $row2['ID'])
		{
			
			echo "<form action='EditAnnouncement.php' method='POST'>";
			echo "    <input type='hidden' name='ID' value = ".$row2[ID]." />";
			echo "    <input type='submit' name='Submit' value ='Edit'/>";
			echo "</form>";
		}
		
		
		// Delete Button (Will only show if you made the announcement)
		//if ( ($rowCur['vehicle_ID'] == $row2['ID']) && ($AccountType == 'R') );
		if ($rowCur != '' || $accountType == 'A')
		{
			
			echo "<form action='DeleteAnnouncement.php' method='POST'>";
			echo "    <input type='hidden' name='ID' value = ".$row2['ID']." />";
			echo "    <input type='submit' name='Submit' value ='Delete'/>";
			echo "</form>";
		}
		
		// Post Notification Button
		if ($AccountType=='R')
		{
			$queryReg = "SELECT * FROM RegisteredUser WHERE LoginID = '$name' ";
			$resultReg = mysql_query($queryReg);
			$rowReg = mysql_fetch_array($resultReg);
			$queryAnn = "SELECT * FROM Announcement WHERE vehicle_ID = '$row2[ID]'";
			$resultAnn = mysql_query($queryAnn);
			$rowAnn = mysql_fetch_array($resultAnn); 
			if ($rowCur['vehicle_ID'] != $row2['ID'])
			{
				echo "<form action='SendNotification.php' method='POST'>";
				echo "    <input type='hidden' name='VehicleID' value = '".$row2['ID']."' />";
				echo "    <input type='hidden' name='RegUserID' value = '".$rowReg['RegisteredUserID']."' />";
				echo "    <input type='hidden' name='AnnID' value = '".$rowAnn['ID']."' />";
				echo "    <input type='submit' name='Send' value ='Notify Seller'/>";
				echo "</form>";
			}
		}
		
		//Query bookmark, if the announcement is already bookmarked, instead of the button
		//just display 'Bookmarked'
		
		$query = "SELECT * FROM Announcement WHERE vehicle_ID = '$row2[ID]'";
		$result = mysql_query($query);
		
		$row3 = mysql_fetch_array($result);
		
		$query = "SELECT * FROM Bookmark WHERE announceID = '$row3[ID]' AND User_LoginID = '$name'";
		$result = mysql_query($query);
		
		$row4 = mysql_fetch_array($result);
		
		//echo "'$row3[ID]' <br>";
		//echo "$query <br>";
		//echo $result;
		
		if ($row4['announceID'] == '' || $AccountType == 'A')
		{
			// Save/Bookmark Button
			if($AccountType == 'R')
			{
				echo "</td><td>";
				echo "<form action='saveAnnouncement.php' method='POST'>";
				echo "    <input type='hidden' name='ID' value = ".$row2['ID']." />";
				echo "    <input type='submit' name='Submit' value ='Bookmark'/>";
				echo "</form>";
			}
			
		}
		else if ($AccountType == 'R')
		{
			echo "</td><td>";
			echo "Bookmarked!";
		}
		
		
		echo "</td></tr>";
		echo "</table>";
	
	

?>

<div id="comments">
<?php
	$commentQuery = "Select * From Comment WHERE VehicleID='".$row2['ID']."' ";
	$commentResult = mysql_query($commentQuery);
	
	while($commentRow = mysql_fetch_array($commentResult))
	{
		echo "<div id='comment'>";
		echo "Comment from ".$commentRow['userID']."<br />";
		echo $commentRow['CommentText']."<br />";
		echo "posted on ".$commentRow['Timestamp']."<br />";
		echo "</div>";
	}
	
	echo "<form action='addComment.php' method='post'>";
	echo "Enter a comment:<br />";
	echo "<textarea name='commentText' rows='4' cols='30'></textarea><br />";
	echo "<input name='ID' type='hidden' value='".$row2['ID']."' />";
	echo "<input name='detailsID' type='hidden' value='".$_GET['detailsID']."' />";
	echo "<input type='submit' value='Post Comment' />";
	echo "</form>";

}
	//mysql_close();
?>



</div>

<!-- Related Announcements goes here. We only want to display this if the user selected view details -->
<?php
	
	if(!$flag) {

	//We want to grab every vehicle that is not the already selected vehicle
	$query = "SELECT * FROM Vehicle WHERE ID <> '".$_GET['detailsID']."'";
	$result4 = mysql_query($query);
	
	//We want the price range of the vehicle with details shown
		//Note: Currently, prices should not be above 80000 or it will bug out!
		$price = $row2['Price'];
		if ($price < 10000)
		{
			//set range from 0 to 10000
			$low = 0;
			$high = 10000;
		}
		else if ($price < 20000)
		{
			//set range from 10000 to 20000
			$low = 10000;
			$high = 20000;
		}
		else if ($price < 30000)
		{
			//set range from 20000 to 30000
			$low = 20000;
			$high = 30000;
		}
		else if ($price < 40000)
		{
			//set range from 30000 to 40000
			$low = 30000;
			$high = 40000;
		}
		else if ($price < 50000)
		{
			//set range from 30000 to 40000
			$low = 40000;
			$high = 50000;
		}
		else if ($price < 60000)
		{
			//set range from 30000 to 40000
			$low = 50000;
			$high = 60000;
		}
		else if ($price < 70000)
		{
			//set range from 30000 to 40000
			$low = 60000;
			$high = 70000;
		}
		else if ($price < 80000)
		{
			//set range from 30000 to 40000
			$low = 70000;
			$high = 80000;
		}
		
		//We want the sellerID of the vehicle with the details shown
		$query = "SELECT * FROM Announcement WHERE vehicle_ID = '".$row2['ID']."' ";
		$result6 = mysql_query($query);
		$annRow = mysql_fetch_array($result6);
		$theSeller = $annRow['userID'];
	
		//echo "selected vehicleID=";
		//echo $row2['ID'];
		//echo "<br> ";
	
	// keeps getting the next row until there are no more to get
	while($relevantRow = mysql_fetch_array($result4))
	{
	
		//echo "vehicle-";
		//echo $relevantRow['ID'] ;
		//echo "-";
		
		
		//We also want sellerID of each vehicle
		$vehicleid = $relevantRow['ID'];
		$query = "SELECT * FROM Announcement WHERE vehicle_ID = $vehicleid";
		$result5 = mysql_query($query);
		$aRow = mysql_fetch_array($result5);
		$sellerName = $aRow['userID'];	
		
		$scoreCount = 0;
	
		//Add dat score!
		if ($relevantRow['Make'] == $row2['Make'])
		{
			$carScore[$relevantRow['ID']] = 2;
			$scoreCount++;
			//echo "added score for make";
			//echo "<br>";
		}
	    if($relevantRow['Model'] == $row2['Model'])
		{
			$carScore[$relevantRow['ID']] += 2;
			$scoreCount++;
			//echo "added score for model";
			//echo "<br>";
		}
		if($relevantRow['Year'] == $row2['Year'])
		{
			$carScore[$relevantRow['ID']] += 1;
			$scoreCount++;
			//echo "added score for Year";
			//echo "<br>";
		}
		if($sellerName == $theSeller)
		{
			$carScore[$relevantRow['ID']] += 1;
			$scoreCount++;
			//echo "added score for seller";
			//echo "<br>";
		}
		if($relevantRow['Cylinder'] == $row2['Cylinder'])
		{
			$carScore[$relevantRow['ID']] += .5;
			$scoreCount++;
			//echo "added score for Cylinder/Engine";
			//echo "<br>";
		}
		if($relevantRow['Drive'] == $row2['Drive'])
		{
			$carScore[$relevantRow['ID']] += .5;
			$scoreCount++;
			//echo "added score for drive type";
			//echo "<br>";
		}
		if ( ($relevantRow['Price'] < $high) && ($relevantRow['Price'] >= $low) )	
		{
			$carScore[$relevantRow['ID']] += 1;
			$scoreCount++;
			//echo "added score for price";
			//echo "<br>";
		}
		//For those with no score
		else if ($scoreCount == 0)
		{
			$carScore[$relevantRow['ID']] = 0;
		}
		
		//echo "-";
		//echo $carScore[$relevantRow['ID']];
		//echo "<br>";
		
		//create a temp max
		//if score is greater than max, set score as first
		//if another score is great, set that to first, set previous as second...

		
		/*
		if ($relevantRow['ID'] == 1)	//first run
		{
			$first = $carScore[$relevantRow['ID']];
			$newArray[0] = $relevantRow['ID'];
		}
		
		else 
		{
			
			if ($carScore[$relevantRow['ID']] > $first)
			{
			
				if ($relevantRow['ID'] = 2)
				{
				
					$newArray[1] = $newArray[0];
					$newArray[0] = $relevantRow['ID'];
					$second = $first;
					$first = $carScore[$relevantRow['ID']];
				}
				else if ($relevantRow['ID'] > 2)
				{
					$newArray[2] = $newArray[1];
					$newArray[1] = $newArray[0];
					$newArray[0] = $relevantRow['ID'];
					$third = $second;
					$second = $first;
					$first = $carScore[$relevantRow['ID']];
				}
			
			}
			else if ($carScore[$relevantRow['ID']] < $first)
			{
				if ($relevantRow['ID'] = 2)
				{
					//$second = $first;
					$newArray[1] = $relevantRow['ID'];
					$second = $carScore[$relevantRow['ID']];
				}
				else if ($relevantRow['ID'] > 2)
				{
					if ($carScore[$relevantRow['ID']] > $second)
					{
						$newArray[2] = $newArray[1];
						$newArray[1] = $relevantRow['ID'];
						$third = $second;
						$second = $carScore[$relevantRow['ID']];
					}
					else
					{
						if ($carScore[$relevantRow['ID']] > $third)
						{
							$newArray[2] = $relevantRow['ID'];
							$third = $carScore[$relevantRow['ID']];
						}
					}
				}
			}
		}
		*/
		
	}
	
	
	arsort($carScore);
	
	$keyArray = array();
	
	$loopCnt = 0;
	
	foreach ($carScore as $key => $val)
	{
		//echo "vehicle[" . $key . "] = " . $val . "\n";
		$keyArray[] = $key;
		if ($loopCnt == 2)
		{
			break;
		}
		$loopCnt++;
	}

	echo "<br>";
	
	//For testing purposes...
	/*
	foreach ($keyArray as $key => $val)
	{
		echo "vehicle[" . $key . "] = " . $val . "\n";
	}
	
	echo "<br>";	
	echo $keyArray[0];
	echo "<br>";
	echo $keyArray[1];
	echo "<br>";
	echo $keyArray[2];	
	
	//Now display similar announcements
	
	$vehicle1ID = $keyArray[0];
	$vehicle2ID = $keyArray[1];
	$vehicle3ID = $keyArray[2];
	*/
	
	if ($AccountType == 'R' || $AccountType == 'A')
	{
	echo "<h1>Similar Announcements</h1>";
	echo "<hr>";
	
	echo "<table border='1'>";
	echo "<tr>  <th>Make</th> <th>Model</th> <th>Year</th> <th>Price</th>";
	echo "</tr>";
	
	
	for ($i=0;$i<3; $i++)
	{
		//'".$row2['ID']."' "
		$query = "SELECT * FROM Vehicle WHERE ID = '".$keyArray[$i]."' ";
		$result6 = mysql_query($query);
		$rowCar = mysql_fetch_array($result6);
		
		echo "<tr><td>"; 
		echo $rowCar['Make'];
		echo "</td><td>"; 
		echo $rowCar['Model'];
		echo "</td><td>"; 
		echo $rowCar['Year'];
		echo "</td><td>"; 
		echo $rowCar['Price'];
		echo "</td><td>"; 
		echo "<form mathod='get' action='announcement.php'><input type='hidden' value='".$rowCar['ID']."' name='detailsID' /><input type='submit' value='Show Details' /></form>";
	
	}
	echo "</td></tr>";	
	echo "</table>";	
	
	
	echo "<br>";
	
	

	}
	
	}
	mysql_close();

?>

<!-- Footer Goes Here -->
<div id="footer">
<hr>
<P>(C)Copyright 2011 Zion's DBA. All Rights Reserved</P>
</div>

</BODY>
</HTML>